Making a public
constant just final
as opposed to static final
leads to duplicating its value for every
instance of the class, uselessly increasing the amount of memory required to execute the application.
Further, when a non-public
, final
field isn’t also static
, it implies that different instances can have
different values. However, initializing a non-static final
field in its declaration forces every instance to have the same value. So such
fields should either be made static
or initialized in the constructor.
Noncompliant code example
public class Myclass {
public final int THRESHOLD = 3;
}
Compliant solution
public class Myclass {
public static final int THRESHOLD = 3; // Compliant
}
Exceptions
No issues are reported on final fields of inner classes whose type is not a primitive or a String. Indeed according to the Java specification:
An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (§8.7)
or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).